package com.erp.module.dingDan

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.module.dingDan.sSDingDan.ProdyctionActivity
import com.erp.module.dingDan.prodyctionClient.ProdyctionClientActivity
import com.erp.myAdapter.OrderProductionAdapter
import com.erp.bean.OrderSousuoBean
import com.erp.bean.ProductionBean
import com.erp.bean.Twostringbean
import com.erp.CaptureActivity
import com.erp.mclass.UnCeHandler
import com.erp.myFragment.BaseKFragment
import com.erp.permission.PermissionListener
import com.erp.R
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import kotlinx.android.synthetic.main.fragment_two.*
import java.util.*

/**
 * Created by Administrator on 2016/11/2 0002.
 * 作者：刘洋
 *  订单查询页面
 */
class FragmentDingDan : BaseKFragment(), View.OnClickListener,DingDanContract.DingDanView {

    //生产单返回的数据内容
    private val f = "FragmentTwo"
    lateinit var productionBeanList: MutableList<ProductionBean>
    lateinit var adapter: OrderProductionAdapter
    var updatedialog: AlertDialog?=null
    lateinit var searchParams: ArrayList<Twostringbean>
    lateinit var time: Twostringbean
    private var select: Int = 0
    lateinit var P : IDingDanP
    private var issousuo:Boolean = false
    private var all :String = ""
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_two
    }

    //加载数据方法
    override fun initData() {
        sousuoBean = OrderSousuoBean()
        productionBeanList = ArrayList<ProductionBean>()
        P = IDingDanP(productionBeanList,context!!,this)
        adapter = OrderProductionAdapter(productionBeanList, activity!!)
        lv_fragment_two.adapter = adapter
        searchParams = ArrayList()
        time = Twostringbean()
        val twostringbean = Twostringbean()
        twostringbean.string = ""
        twostringbean.name = ""
        twostringbean.mstring = ""
        initClick()
        searchParams.add(twostringbean)
        P.gainData("production")
        if(select==0) {
            P.sousuo(sousuoBean)
            Pull.headerRefreshing()
        }
        if(issousuo){
            update()
        }
    }

    override fun xialaOk() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
        if (select == 3 || select == 4 || select == 5 && productionBeanList.size != 0) {
            val intent = Intent(activity, ProdyctionClientActivity::class.java)
            intent.putExtra("id", productionBeanList[0].id)
            intent.putExtra("select", select)
            intent.putExtra("all", sousuoBean.all)
            startActivity(intent)
        }
    }

    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
    }

    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }
    private fun settextsousuo() {
        val sousuo = StringBuffer("")
        if(sousuoBean.all!=""){
            sousuo.append("所有"+ ":"+ sousuoBean.all)
        }
        if(sousuoBean.onum!=""){
            sousuo.append("订单号"+ ":"+ sousuoBean.onum)
        }
        if(sousuoBean.o_contract_num_like!=""){
            sousuo.append("销售订单号"+ ":"+ sousuoBean.o_contract_num_like)
        }
        if(sousuoBean.o_customer_like!=""){updatedialog
            sousuo.append("客户名"+ ":"+ sousuoBean.o_customer_like)
        }
        if(sousuoBean.province!=""){
            sousuo.append("省份"+ ":"+ sousuoBean.province)
        }
        if(sousuoBean.area!=""){
            sousuo.append("区域"+ ":"+ sousuoBean.areas)
        }
        if(sousuoBean.o_type!=""){
            sousuo.append("类型"+ ":"+ sousuoBean.o_types)
        }
        if(sousuoBean.is_canceled!=""){
            if(sousuoBean.is_canceled=="yes"){
                sousuo.append("状态"+ ":"+"是")
            }else {
                sousuo.append("状态" + ":" + "否")
            }
        }
        if(sousuoBean.field_empty!=""){
            sousuo.append("为空"+ ":"+ sousuoBean.field_emptys)
        }
        if(sousuoBean.field_not_empty!=""){
            sousuo.append("非空"+ ":"+ sousuoBean.field_not_emptys)
        }
        if(sousuoBean.shop_name!=""){
            sousuo.append("专卖店"+ ":"+ sousuoBean.shop_name)
        }
        if(sousuoBean.special_service_cat.size>0) {
            sousuo.append("特服类型：")
            for (j in 0 until sousuoBean.special_service_cat.size) {
                if (j == sousuoBean.special_service_cat.size - 1) {
                    sousuo.append(sousuoBean.special_service_cat[j])
                } else {
                    sousuo.append(sousuoBean.special_service_cat[j])
                    sousuo.append(" ")
                }
            }
        }
        val times = arrayOf("o.ctime", "o.sqxd_time", "o.cd_time", "o.quoted_price_time", "o.charge_audit_time", "o.warehousing_time", "o.deliver_time")
        val timess = resources.getStringArray(R.array.istime)
        if (sousuoBean.time_category.trim()!="") {
            sousuo.append(sousuoBean.time_categorys).append(sousuoBean.start_time).append("---").append(sousuoBean.end_time)
            for (i in timess.indices) {
                if (sousuoBean.time_category == times[i]) {
                    sousuo.append(timess[i] + ":").append(sousuoBean.start_time).append("---").append(sousuoBean.end_time)
                }
            }
        }
        tv_query.text = sousuo.toString()
    }

    private fun initClick() {
        LlQingKong.setOnClickListener(this)
        iv_scan.setOnClickListener(this)
        tv_query.setOnClickListener(this)
        lv_fragment_two.onItemClickListener = AdapterView.OnItemClickListener { _, _, i, _ ->
            val intent = Intent(activity, ProdyctionClientActivity::class.java)
            intent.putExtra("id", productionBeanList[i].id)
            intent.putExtra("selsct", select)
            intent.putExtra("all", "")
            startActivity(intent)
        }
        Pull.setheader(true)
        Pull.setOnHeaderRefreshListener {
            select=0
            P.xiala()
        }
        Pull.setOnFooterRefreshListener { P.shangla()}

    }

    fun setSelect(select: Int){
        this.select = select
    }

    fun setUpdate(s: String ){
        this.all = s
        issousuo = true
    }

    fun update() {
        sousuoBean = OrderSousuoBean()
        sousuoBean.all=all
        settextsousuo()
        updatedialog = Myutil.showupdatedialog(activity, "正在搜索", false)
        P.sousuo(sousuoBean)
        P.xiala()
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        UnCeHandler.buzhou = "onActivityResult"
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        when (requestCode) {
            1 -> if (resultCode == Activity.RESULT_OK) {
                val bundle = data.extras
                //显示扫描到的内容
                var s: String = bundle?.get("result") as String
                Myutil.MyTost(s)
                sousuoBean = OrderSousuoBean()
                sousuoBean.all=s
                select=0
                settextsousuo()
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.sousuo(sousuoBean)
                P.xiala()
                LlQingKong.visibility = View.VISIBLE
            }
            101 -> if (resultCode == Activity.RESULT_OK) {
                select=0
                settextsousuo()
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.sousuo(sousuoBean)
                P.xiala()
                LlQingKong.visibility = View.VISIBLE
            }
        }
    }

    override fun onClick(v: View) {
        when (v) {
            iv_scan -> PermissionUtils.issaomiao(activity!!, object : PermissionListener {
                override fun onSucceed(requestCode: Int, grantPermissions: List<String>) {
                    val intent = Intent()
                    intent.setClass(activity!!, CaptureActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    startActivityForResult(intent, 1)
                }

                override fun onFailed(requestCode: Int, deniedPermissions: List<String>) {
                    Myutil.MyTost("没有权限，无法扫描")
                }
            })
            LlQingKong -> {
                qingkong()
            }
            tv_query -> {
                val intent = Intent(activity, ProdyctionActivity::class.java)
                startActivityForResult(intent, 101)
                //               startActivity(intent);
                activity!!.overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
            }
        }
    }

    fun qingkong(){
        if(isFirstLoad){
            return
        }
        sousuoBean = OrderSousuoBean()
        select = 0
        settextsousuo()
        P.sousuo(sousuoBean)
        Pull.headerRefreshing()
        LlQingKong.visibility = View.GONE
    }

    companion object {
        var sousuoBean = OrderSousuoBean()
    }
}
